package rexsee.natives;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.browser.clazz.RootViewInterface;
import rexsee.core.utilities.Utilities;
import android.app.Activity;
import android.content.Context;

public class RexseeJavaBar implements JavascriptInterface {

	public static final String INTERFACE_NAME = "JavaBar";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	public static final String ERROR_BAR_NOT_EXISTS = "The bar specified by the id does not exist.";

	private final RootViewInterface mLayout;
	private final Browser mBrowser;
	private final Context mContext;
	private final HashMap<String, JavaBar> mMap;
	private final RexseeJavaView mViews;
	private boolean syncTag = false;

	public RexseeJavaBar(RootViewInterface rootView, RexseeJavaView views) {

		super();
		mLayout = rootView;
		mBrowser = rootView.getBrowser();
		mContext = mBrowser.getContext();
		mMap = new HashMap<String, JavaBar>();
		mViews = views;

	}

	private void _remove(String id) {
		if (!mMap.containsKey(id)) return;
		final JavaBar bar = mMap.get(id);
		if (bar != null) {
			syncTag = false;
			((Activity) mContext).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					try {
						bar.removeChilds();
						mLayout.removeChild(bar.styleSheet.bar_position, bar);
					} catch (Exception e) {
						mBrowser.exception(getInterfaceName(), e);
					}
					syncTag = true;
				}
			});
			while (!syncTag) {
				try {
					Thread.sleep(100);
				} catch (Exception e) {

				}
			}
		}
	}

	//JavaScript Interface

	//Operation on bar

	public void add(final String id, final String style) {
		if (id == null || id.trim().equalsIgnoreCase("")) {
			mBrowser.exception(getInterfaceName(), "The id of bar should not be null.");
			return;
		}
		if (mMap.containsKey(id)) {
			mBrowser.exception(getInterfaceName(), "The specified view has already been shown.");
			return;
		}
		final ViewTag viewTag = mViews.getViewTag(id);
		if (viewTag == null) {
			mBrowser.exception(getInterfaceName(), "The specified view is null.");
			return;
		}
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					JavaBar bar = new JavaBar(mBrowser, style, viewTag);
					mMap.put(id, bar);
					mLayout.addChild(bar.styleSheet.bar_position, Utilities.getInt(bar.styleSheet.index, -1), bar);
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public void remove(String id) {
		if (!mMap.containsKey(id)) return;
		_remove(id);
		mMap.remove(id);
	}
	public void removeAll() {
		Set<String> keys = mMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			_remove(iterator.next());
		}
		mMap.clear();
	}
	public void setStyle(final String id, final String style) {
		if (id == null || id.trim().equalsIgnoreCase("") || !mMap.containsKey(id)) {
			mBrowser.exception(getInterfaceName(), "The bar specified by the id does not exist.");
			return;
		}
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					mMap.get(id).setStyleWithoutLayoutParams(style);
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}
	public String getStyle(String id, String name) {
		return (!mMap.containsKey(id)) ? "" : mMap.get(id).getStyle(name);
	}
	public boolean exists(String id) {
		return mMap.containsKey(id);
	}
	public int size() {
		return mMap.size();
	}
	public String getIds() {
		String rtn = "";
		Set<String> ids = mMap.keySet();
		Iterator<String> iterator = ids.iterator();
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + iterator.next() + "\"";
		}
		return "[" + rtn + "]";
	}

}
